Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

should-update

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

should-update

A shouldComponentUpdate-wrapper

  • 2.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
242
decreased by-5.84%
Maintainers
1
Weekly downloads
 
Created
Source

should-update

SYNOPSIS

One of the main mistakes in a react component is, that the component does not include a shouldComponentUpdate function. Therefore, all the changes made to a components state or props led to a re-render.

If there is a shouldComponentUpdate function, it mostly looks like this:

class Example extends Component {
  shouldComponentUpdate(nextProps) {
    return (
      this.props.id !== nextProps.id ||
      this.props.name !== nextProps.name ||
      this.props.something !== nextProps.something
    );
  }
}

At least, this function will grow as hell if the component held more data.

class Example extends Component {
  shouldComponentUpdate(nextProps) {
    return (
      this.props.id !== nextProps.id ||
      this.props.name !== nextProps.name ||
      this.props.something !== nextProps.something ||
      this.props.a !== nextProps.a ||
      this.props.b !== nextProps.b ||
      this.props.c !== nextProps.c ||
      this.props.some.id !== nextProps.some.id
      // ...
    );
  }
}

Hmmm... pretty ugly stuff, isn't it? With should-update you can reduce the code of the shouldComponentUpdate lifecycle method to a minimum.

Note, that should-update also accepts nested data. To compare it you need to separate the keys with a dot (e.g. some.id or user.email.main).

import { shouldUpdate } from 'should-update';

class Example extends Component {
  shouldComponentUpdate(nextProps) {
    return shouldUpdate(
      ['id', 'name', 'something', 'a', 'b', 'c', 'some.id'],
      this.props,
      nextProps
    );
  }
}

or:

import { createShouldUpdate } from 'should-update';

class Some {
  shouldComponentUpdate: createShouldUpdate('some', 'value')
}

API

shouldUpdate(alterable, props, upcomingProps)

  • alterable - The changeable props.
  • props - The current props (this.props).
  • nextProps - The upcoming props (nextProps).

createShouldUpdate(...alterable)

  • alterable - The changeable props.

Install

npm i --save should-update

License

The MIT License (MIT)

Copyright (c) 2016 Marc Binder marcandrebinder@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Keywords

FAQs

Package last updated on 30 May 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc